<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/6/13
 * Time: 15:51
 */

if (!defined('IN_IA')) {
    exit('Access Denied');
}

class Index_MxShopPage extends WebPage {

    public function getType ()
    {
        return  array('type' => 0, 'name'=>'平台负责人');
    }

    public function main() {
        global $_W, $_GPC;
        $uniacid = $_W['platformid'];

        $pindex = max(1, intval($_GPC['page']));
        $psize = 20;
        $keyword = $_GPC['keyword'];
        $condition = ' p.uniacid=:uniacid AND p.deleted=0';
        $params = array(':uniacid'=>$uniacid);
        if (!empty($keyword)) {
            $condition .= ' AND (locate(:keyword,m.nickname)>0 OR locate(:keyword,p.name)>0)';
            $params[':keyword'] = htmlspecialchars_decode($_GPC['keyword'], ENT_QUOTES);
        }
        $status = intval($_GPC['status']);
        if (!empty($status)) {
            if ($status == 2) {
                $status2 = 0;
            } else {
                $status2 = $status;
            }
            $condition .= ' AND p.status=:status';
            $params[':status'] = $status2;
        }



        $list = pdo_fetchall("SELECT p.*, m.nickname, m.account, m.thumb as avatar FROM " . tablename('mx_shop_platform') . " p LEFT JOIN " . tablename('mx_shop_role_manage') ." m ON m.id=p.manageid WHERE " . $condition . " ORDER BY p.displayorder DESC,p.id ASC LIMIT " . ($pindex - 1) * $psize . ',' . $psize, $params);
        $total = pdo_fetchcolumn("SELECT count(p.id) FROM " . tablename('mx_shop_platform') . " p LEFT JOIN " . tablename('mx_shop_role_manage') ." m ON m.id=p.manageid WHERE " . $condition, $params);

        $pager = pagination($total, $pindex, $psize);

        include $this->template();
    }

    public function post(){

        global $_W, $_GPC;
        $uniacid = $_W['platformid'];

        $id = intval($_GPC ['id']);
        $types = $this->getType();

        if (!empty ($id)) {
            $item = pdo_fetch("SELECT * FROM " . tablename('mx_shop_platform') . " WHERE id = '$id'");
            $manage = pdo_fetch("SELECT * FROM " . tablename('mx_shop_role_manage') . " WHERE id=:id AND type=:type AND deleted=0", array(':id' => $item['manageid'], ':type' => $types['type']));

        } else {
            $item = array(
                'displayorder' => 0,
                'thumb' => '',
                'status' => 1
            );
        }

        if ($_W['ispost']) {

            if (empty ($_GPC ['catename'])) {
                show_json(0, '抱歉，请输入平台名称！');
            }

            $mcname = pdo_fetch("SELECT id FROM " . tablename('mx_shop_platform') . " WHERE id != :id AND name = :mcname AND deleted=0", array(':id' => $id, ':mcname' => $_GPC['catename']));
            if(!(empty($mcname['id']))){
                show_json(0,"该平台名称已存在");
            }

            if (empty($_GPC['nickname'])) {
                show_json(0,"请输入投资人姓名");
            }

            if (empty($_GPC['account'])) {
                show_json(0,"请输入账号");
            }

            $condition = 'uniacid=:uniacid AND id != :id AND deleted=0 AND type=:type AND (nickname=:nickname OR account=:account)';
            $params = array(
                ':uniacid' => $_W['platformid'],
                ':id' => $item['manageid'] || 0,
                ':type' => $types['type'],
                ':nickname' => $_GPC['nickname'],
                ':account' => $_GPC['account']
            );

            $acc = pdo_fetch("SELECT id FROM " . tablename('mx_shop_role_manage') . " WHERE ".$condition, $params);
            if(!(empty($acc['id']))){
                show_json(0,"该负责人或账号已存在");
            }

            $account = array(
                'uniacid' => $uniacid,
                'nickname' => trim($_GPC['nickname']),
                'type' => $types['type'],
                'thumb' => $_GPC['avatar'],
                'account' => trim($_GPC['account']),
                'status' => intval($_GPC['status']),
                'updatetime' => TIMESTAMP,
            );

            $data = array(
                'uniacid' => $uniacid,
                'name' => $_GPC ['catename'],
                'description' => $_GPC ['description'],
                'status' => intval($_GPC ['status']),
                'thumb' => $_GPC ['thumb'],
                'displayorder' => intval($_GPC ['displayorder']),
                'updatetime' => TIMESTAMP
            );
            if (!empty ($id)) {

                if(empty($manage) || !empty($_GPC['password']) )
                {
                    if(strlen($_GPC['password']) < 6 || strlen($_GPC['password']) >20)
                    {
                        show_json(0, "请输入6~20位的密码");
                    }

                    $salt = random(32);
                    $account['password'] = md5(md5($_GPC['password']).$salt);
                    $account['salt'] = $salt;
                }

                if(empty($manage))
                {
                    pdo_insert('mx_shop_role_manage', $account);
                    $accid = pdo_insertid();
                    $data['manageid'] = $accid;
                }
                else
                {
                    pdo_update('mx_shop_role_manage', $account, array('id' => $item['manageid']));
                }

                pdo_update('mx_shop_platform', $data, array('id' => $id));
                plog('platform.add', '更新"' . $data['name'] . '"平台成功！平台ID为' . $id);
            }
            else
            {
                if(empty($_GPC['password']) || strlen($_GPC['password']) < 6 || strlen($_GPC['password']) >20)
                {
                    show_json(0,"请输入6~20位的密码");
                }

                $salt = random(32);
                $account['password'] = md5(md5($_GPC['password']).$salt);
                $account['salt'] = $salt;
                $account['createtime'] = TIMESTAMP;

                pdo_insert('mx_shop_role_manage', $account);
                $accid = pdo_insertid();

                $data['manageid'] = $accid;
                $data['createtime'] = $data['updatetime'];
                pdo_insert('mx_shop_platform', $data);
                $id = pdo_insertid();
                plog('platform.add', '新建"' . $data['name'] . '"平台成功！平台ID为' . $id);
            }
            show_json(1, array('url'=>webUrl('platform')));

        }

        include $this->template();
    }

    public function status(){

        global $_W, $_GPC;

        $id = intval($_GPC['id']);
        $status = intval($_GPC['status']);

        if(!empty($id)){
            $item = pdo_fetch("SELECT id,uniacid,manageid,name FROM " . tablename('mx_shop_platform') . " WHERE id=:id AND uniacid=:uniacid AND deleted=0", array(':id'=>$id, ':uniacid'=>$_W['platformid']));

            if($status == 1)
            {
                pdo_update('mx_shop_role_manage', array( 'status' => 1), array('id' => $item['manageid'], 'uniacid'=>$item['uniaciod']));
                pdo_update('mx_shop_platform', array( 'status' => 1), array('id' => $id));
            }
            else
            {
                pdo_update('mx_shop_role_manage', array( 'status' => 0), array('id' => $item['manageid'], 'uniacid'=>$item['uniaciod']));
                pdo_update('mx_shop_platform', array( 'status' => 0), array('id' => $id));
            }
        }
        show_json(1, array('url'=>webUrl('platform')));

    }

    public function delete(){

        global $_W, $_GPC;

        $id = intval($_GPC['id']);
        if (empty($id)) {
            $id = is_array($_GPC['ids']) ? implode(',', $_GPC['ids']) : 0;
        }

        $md = pdo_fetchall("SELECT id FROM " . tablename('mx_shop_mendian_industry') . " WHERE platformid in( $id ) AND deleted=0 AND uniacid=" . $_W['platformid']);
        if(!empty($md)){
            show_json(0, '要删除该平台，需删除该平台下的行业');
        }

        $cates = pdo_fetchall("SELECT * FROM " . tablename('mx_shop_platform') . " WHERE id in( $id ) AND uniacid=" . $_W['platformid']);
        $types = $this->getType();
        foreach ($cates as $cate) {
            pdo_update('mx_shop_role_manage', array('deleted'=>1), array('id' => $cate['manageid'], 'type' => $types['type'], 'uniacid'=>$_W['platformid']));
            pdo_update('mx_shop_platform', array('deleted' => 1, 'status' => 0), array('id' => $cate['id'], 'uniacid'=>$_W['platformid']));
            plog('platform.delete', "删除平台 <br/>平台信息:  ID: {$cate['id']}");
        }
        show_json(1, array('url' => referer()));

    }

    public function bonus(){

        global $_W,$_GPC;
        $uniacid = $_W['platformid'];
        $id = intval($_GPC['id']);

        if(empty($id)){
            show_json(0,'参数有误！');
        }

        $platform = pdo_fetch("SELECT id,name FROM " . tablename('mx_shop_platform') . " WHERE uniacid=:uniacid AND id = :id AND deleted=0", array(':uniacid'=>$uniacid,':id' => $id));
        if(empty($platform)){
            show_json(0,'抱歉，该平台不存在！');
        }
        $item = pdo_fetch("SELECT * FROM " . tablename('mx_shop_mendian_bonus') . " WHERE uniacid=:uniacid AND industryid = :industryid ", array(':uniacid'=>$uniacid, ':industryid' => $id));

        if ($_W['ispost']) {
            $num = abs($_GPC['tuijian2']) + abs($_GPC['tuijian3']) + abs($_GPC['shengji']) + abs($_GPC['quyu']) + abs($_GPC['hangye']) + abs($_GPC['jishu']) + abs($_GPC['tuijian']) + abs($_GPC['xiaofei']) + abs($_GPC['gongsi']) + abs($_GPC['jiangjin']);
            if(ceil($num) > 100){
                show_json(0,'行业分红不能超过100%！');
            }

            $data = array(
                'uniacid' => $uniacid,
                'shengji' => abs($_GPC['shengji']),
                'quyu' => abs($_GPC['quyu']),
                'hangye' => abs($_GPC['hangye']),
                'jishu' => abs($_GPC['jishu']),
                'tuijian' => abs($_GPC['tuijian']),
                'tuijian2' => abs($_GPC['tuijian2']),
                'tuijian3' => abs($_GPC['tuijian3']),
                'xiaofei' => abs($_GPC['xiaofei']),
                'gongsi' => abs($_GPC['gongsi']),
                'jiangjin' => abs($_GPC['jiangjin']),
                'updatetime' => time()
            );

            if (empty($item['id'])) {
                $data['industryid'] = $id;
                $data['createtime'] = time();
                pdo_insert('mx_shop_mendian_bonus', $data);
                $bid = pdo_insertid();
                plog('platform.bonus', '新建"' . $platform['name'] . '"行业分红成功！行业分红ID为' . $bid);
            } else {
                pdo_update('mx_shop_mendian_bonus', $data, array('id' => $item['id']));
                plog('platform.bonus', '更新"' . $platform['name'] . '"行业分红成功！行业分红ID为' . $item['id']);
            }
            show_json(1, array('url'=>webUrl('platform')));
        }

        include $this->template();

    }

}
